<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

use Illuminate\Database\Query;
// require_once '../app/libary/captcha/Code.class.php';

class AreaController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/area/index';
    protected $typeleve=1;
    protected $typeleve_default=1;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    // 地區管理
    public function index(){
        $areas = BackstageModel::getAreaList();
        $list = CommonModel::getList('area',['id','name','pid','sort_order'],['pid'=>0]);
        return view('backstage/area/index',['list'=>$list,'areas'=>$areas]);
    }
    public function doadd(){
        if($request->isMethod('POST')){
            $data = $request->input();
            $area['pid'] = $data['pid'];
            $area['name'] = $data['name'];
            if(isset($data['id']) && $data['id']!=0){
                // 修改
                $id = $data['id'];
                $save = CommonModel::updateData('area',['id'=>$id],$area);
                if($save){
                    return response()->json(array('status'=>1,'message'=>'編輯成功'));
                }else{
                    return response()->json(array('status'=>0,'message'=>'編輯失敗'));
                }
            }else{
                // 添加
                $newid = CommonModel::addData('area',$area);
                if($newid){
                    return response()->json(array('status'=>1,'message'=>'添加成功'));
                }else{
                    return response()->json(array('status'=>0,'message'=>'添加失敗'));
                }
            }
        }
    }
    // 批量添加地區頁面
    public function adds(){
        // $areas = DB::table('area')->select(['id','name'])->get();
        $areas = BackstageModel::getAreaList();
        // $areas = CommonModel::getField('area',"'id','name'");
        // $areas = DB::table('area')->pluck('id','name','pid','sort_order');
        // dd($areas);exit;
        return view('backstage/area/adds',['areas'=>$areas]);
    }
    public function doadds(Request $request){
        if($request->isMethod('POST')){
            $data = $request->input();
            $pid = $data['pid'];
            $area_arr = explode('，',$data['area_name']);
            $x = 0;
            $y = 0;
            foreach ($area_arr as $vv) {
                $res = CommonModel::addData('area',['pid'=>$pid,'name'=>$vv]);
                if($res){
                    $x++;
                }else{
                    $y++;
                }
            }
            if($x==0){
                return response()->json(array('status'=>0,'message'=>'添加失敗','jumpUrl'=>'/backstage/area/index'));
            }elseif(($y==0)){
                return response()->json(array('status'=>1,'message'=>'添加成功',));
            }else{
                return response()->json(array('status'=>1,'message'=>'成功:'.$x.',失敗：'.$y,'jumpUrl'=>'/backstage/area//index'));
            }
        }
    }
    // 獲取地區列表
    public function getArea(){
        $pid = intval($_POST['id']);
        $count = CommonModel::getCount('area',['pid'=>$pid]);
        if($count==0){
            return response()->json(array('status' => 0,'message'=>'失敗或沒有數據'));
        }else{
            $list = CommonModel::getList('area',['id','name','pid','sort_order'],['pid'=>$pid]);
            $s = '';
            foreach($list as $key=>$vb){
                $leve = $this->_typeLeve($vb->id);
                $counts = CommonModel::getCount('area',['pid'=>$vb->id]);
                $s .= '<tr id="list_'.$vb->id.'" class="leve_'.$leve.'" typeid="'.$vb->id.'" parentid="'.$vb->pid.'" flag="0" val="'.$vb->name.'">';
                $s .= '<td class="tc"><input type="checkbox" name="checkbox" value="'.$vb->id.'"></td>
                            <td class="tc">'.$vb->id.'</td>';
                if($counts==0){
                    $s .='<td class="checkSon">'.$vb->name.'</td>';
                }else{
                    $s .='<td class="checkSon"><i class="fa fa-plus-square-o" style="cursor:pointer;" data="son"></i>'.$vb->name.'</td>';
                }
                $s .= '<td>'.$vb->sort_order.'</td>
                            <td>
                                <a href="javascript:void(0);" onclick="openRim('.$vb->id.');">編輯</a>
                                <a href="javascript:void(0);" onclick="del('.$vb->id.');">刪除</a>
                            </td>
                        </tr>';
            }
            $data['inner'] = $s;
            $data['typeid'] = $pid;
            return response()->json(array('status' => 1,'message'=>'成功','data'=>$data));
        }
    }
    
    // 獲取地區級別
    protected function _typeLeve($typeid){
        static $rt=0;//先聲明要返回靜態變量,不然在下面被賦值時是引用賦值
        $pid = CommonModel::getField('area','pid',['id'=>$typeid]);
        if($pid>0){
            $this->typeleve++;
            $this->_typeLeve($pid);
        }else{
            $rt = $this->typeleve;
            $this->typeleve = $this->typeleve_default;
        }
        return $rt;
    }
    


}